package client;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import util.ClientUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class Client extends ClientUtil {


    public static void main(String[] args) {
        new Client().getIP();
    }


    public void getIP(){
        super.proxyConfig();

        // 拼接请求，尝试请求百度首页
        String url = "http://2474.replace.favo.fengmeng.net/";

        // 设置请求头文件
        Header[] headers = new Header[]{
                new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"),
                new BasicHeader("Connection", "keep-alive"),
                new BasicHeader("Host", "2474.replace.favo.fengmeng.net"),
                new BasicHeader("Upgrade-Insecure-Requests:", "1"),
                new BasicHeader("accept-encoding:", "gzip, deflate, br")
        };
        try {
            HttpUriRequest req = getHttpUriRequest("get",url,headers,null);
            CloseableHttpResponse response = getResp(req);
            HttpEntity entity = response.getEntity();


            url = "https://s19.cnzz.com/z_stat.php?id=1263556672&web_id=1263556672";
            req = getHttpUriRequest("get",url,headers,null);
            response = getResp(req);
            entity = response.getEntity();


            url = "https://c.cnzz.com/core.php?web_id=1263556672&t=z";
            req = getHttpUriRequest("get",url,headers,null);
            response = getResp(req);
            entity = response.getEntity();

            url = "https://z8.cnzz.com/stat.htm?id=1263556672&r=&lg=zh-cn&ntime=none&cnzz_eid=181077886-1637067969-&showp=1366x768&p=http%3A%2F%2F2474.replace.favo.fengmeng.net%2F&t=&umuuid=17d292d399e87-0f0afaa12e5c81-57b1a33-100200-17d292d399f468&h=1&rnd=439262580";
            req = getHttpUriRequest("get",url,headers,null);
            response = getResp(req);
            entity = response.getEntity();

            // 获取到可以使用的cookie
            url = "https://cnzz.mmstat.com/9.gif?abc=1&rnd=38590523";
            req = getHttpUriRequest("get",url,headers,null);
            response = getResp(req);
            entity = response.getEntity();
            Header[] headers1 = response.getAllHeaders();
            for (Header h : headers1) {
                if ("set-cookie".equalsIgnoreCase(h.getName())) {
                    System.out.println("获取可用cookie："+h.getValue());
                }
            }


            url = "https://www.baidu.com/?tn=40020637_10_oem_dg";
            req = getHttpUriRequest("get",url,headers,null);
            response = getResp(req);
            entity = response.getEntity();
            Header[] headers2 = response.getAllHeaders();
            for (Header h : headers1) {
                if ("set-cookie".equalsIgnoreCase(h.getName())) {
                    System.out.println("获取可用cookie："+h.getValue());
                }
            }

            String str = EntityUtils.toString(entity);
            System.out.println(str);

            // TODO








            // 释放资源
            response.close();
            InputStream in=entity.getContent();
            in.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
